package com.xxl.job.hansonxxljob.config;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Hanson
 * @date 2023/2/13 16:43
 */
@Configuration
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class XxlJobConfig {
    private final Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);

    private final XxlJobBeanConfig bean;

    @Bean
    public XxlJobSpringExecutor xxlJobExecutor() {
        logger.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(bean.getAdmin().getAddresses());
        xxlJobSpringExecutor.setAppname(bean.getExecutor().getAppName());
        xxlJobSpringExecutor.setAddress(bean.getAdmin().getAddresses());
        xxlJobSpringExecutor.setIp(bean.getExecutor().getIp());
        xxlJobSpringExecutor.setPort(bean.getExecutor().getPort()+1000); //端口向后偏移1000
        xxlJobSpringExecutor.setAccessToken(bean.getAccessToken());
        xxlJobSpringExecutor.setLogPath(bean.getExecutor().getLogPath());
        xxlJobSpringExecutor.setLogRetentionDays(bean.getExecutor().getLogRetentionDays());

        return xxlJobSpringExecutor;
    }
}

